package shan.rpc.example.provider;

import shan.rpc.example.common.service.UserService;
import shan.rpc.yurpc.RpcApplication;
import shan.rpc.yurpc.config.RegistryConfig;
import shan.rpc.yurpc.config.RpcConfig;
import shan.rpc.yurpc.model.ServiceMetaInfo;
import shan.rpc.yurpc.registry.LocalRegistry;
import shan.rpc.yurpc.registry.Registry;
import shan.rpc.yurpc.registry.RegistryFatory;
import shan.rpc.yurpc.server.HttpServer;
import shan.rpc.yurpc.server.VertxHttpServer;

public class ProviderExample {

    public static void main(String[] args) {
        RpcApplication.init();

        //注册服务
        LocalRegistry.register(UserService.class.getName(),UserServiceImpl.class);

        //注册服务到注册中心
        RpcConfig rpcConfig=RpcApplication.getRpcConfig();
        RegistryConfig registryConfig=rpcConfig.getRegistryConfig();
        Registry registry= RegistryFatory.getInstance(registryConfig.getRegistry());
        ServiceMetaInfo serviceMetaInfo=new ServiceMetaInfo();
        serviceMetaInfo.setServiceName(UserService.class.getName());
        serviceMetaInfo.setServiceAddress(rpcConfig.getServerHost()+":"+rpcConfig.getServerPort());
        try{
            registry.register(serviceMetaInfo);
        }catch (Exception e){
            throw new RuntimeException(e);
        }

        //启动web服务
        HttpServer httpServer=new VertxHttpServer();
        httpServer.dostart(RpcApplication.getRpcConfig().getServerPort());
    }
}
